package com.lei.Proxy.dynamic;

import java.lang.reflect.Proxy;
import java.util.Random;

/**
 * @author: leiGer
 * @Date: 2021/01/16
 * @Time: 22:48
 */

//坦克类实现一个Movable借口，并实现里面的move方法
public class Tank implements Movable {
    @Override
    public void move() {
        System.out.println("Tank moving clsclacla...");
        try {
            Thread.sleep(new Random().nextInt(10000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Tank tank = new Tank();
        //System.getProperties().put("jdk.proxy.ProxyGenerator.saveGeneratedFiles","true");
        //生成$Proxy0.class
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

        /**
         * 参数一：被代理类的类加载器
         * 参数二：代理接口类数组
         * 参数三：要实现的处理方法
         */
        Movable m = (Movable) Proxy.newProxyInstance(Tank.class.getClassLoader(),
                new Class[]{Movable.class},
                new LogHandler(tank));
        m.move();
    }
}
